//
//  QMViewModel.swift
//  SwiftJson源码
//
//  Created by wsj on 2018/3/24.
//  Copyright © 2018年 wsj. All rights reserved.
//

import UIKit
enum QMClassType {
    case view
    case label
    case button
}
class QMViewModel: NSObject {
    public var wj_view:UIView?
    var type : QMClassType = .view;
    
    
    
    
    /// 判断当前类型是什么
    ///
    /// - Parameter wj_view: 当前的view
    /// - Returns:
    fileprivate func wj_judgeViewType(wj_view:UIView?)->QMClassType{
        if type != .view {
            return type;
        }
        switch wj_view {
        case is UILabel:
            return .label
        case  is UIButton:
            return .button
        default:
            return .view
        }
    }
   
}
// MARK: Frame
extension QMViewModel {
    
    /// 设置view的frame
    ///
    /// - Parameter rect: view的frame
    /// - Returns:
    func wj_frame(_ rect:CGRect) -> QMViewModel {
        assert(self.wj_view != nil, "还没有创建view，怎么设置Frmae");
        self.wj_view?.frame = rect;
        return self;
    }
    
    /// 设置view的size
    ///
    /// - Parameter qm_size: size
    /// - Returns:
    func wj_size(size qm_size:CGSize) -> QMViewModel {
        assert(self.wj_view != nil, "还没有创建view，怎么设置size");
        self.wj_view?.size = qm_size;
        return self;
    }
    
    /// 设置view的宽度
    ///
    /// - Parameter qm_width: 宽度
    /// - Returns:
    func wj_width(width qm_width:CGFloat) -> QMViewModel {
        assert(self.wj_view != nil, "还没有创建view，怎么设置width");
        self.wj_view?.width = qm_width;
        return self;
    }
    
    /// 设置view的高度
    ///
    /// - Parameter qm_height: 高度
    /// - Returns:
    func wj_height(height qm_height:CGFloat) -> QMViewModel {
        assert(self.wj_view != nil, "还没有创建view，怎么设置height");
        self.wj_view?.height = qm_height;
        return self;
    }
    
    /// 设置view的X值
    ///
    /// - Parameter qm_x: x值
    /// - Returns:
    func wj_x(x qm_x:CGFloat) -> QMViewModel {
        assert(self.wj_view != nil, "还没有创建view，怎么设置x");
        self.wj_view?.x = qm_x;
        return self;
    }
    
    /// 设置view的Y值
    ///
    /// - Parameter qm_y: y 值
    /// - Returns:
    func wj_y(y qm_y:CGFloat) -> QMViewModel {
        assert(self.wj_view != nil, "还没有创建view，怎么设置y");
        self.wj_view?.y = qm_y;
        return self;
    }
    
    /// 设置view的centerX
    ///
    /// - Parameter qm_cx: centerX
    /// - Returns:
    func wj_centerX(cx qm_cx:CGFloat) -> QMViewModel {
        assert(self.wj_view != nil, "还没有创建view，怎么设置y");
        self.wj_view?.centerX = qm_cx;
        return self;
    }
    
    ///设置View的centerY
    ///
    /// - Parameter qm_cy:centerY
    /// - Returns:
    func wj_centerY(cy qm_cy:CGFloat) -> QMViewModel {
        assert(self.wj_view != nil, "还没有创建view，怎么设置y");
        self.wj_view?.centerY = qm_cy;
        return self;
    }
    
}
// MARK: View
extension QMViewModel {
    /// 将创建的view 添加到父试图上面
    ///
    /// - Parameter superView: 父试图
    /// - Returns:
    func wj_addSuperView(_ superView:UIView?) -> UIView? {
        assert(self.wj_view != nil || superView != nil, "父试图或者当前试图为nil，添加什么");
        superView?.addSubview(self.wj_view!);
        return self.wj_view;
        
    }
    
    /// 设置viewTag
    ///
    /// - Parameter tagInt: view的Tag
    /// - Returns: 返回当前model
    func wj_tag(_ tagInt:Int) -> QMViewModel {
        guard self.wj_view != nil else {
            return self;
        }
        self.wj_view?.tag = tagInt;
        return self;
    }
    
    /// 设置当下view的背景颜色
    ///
    /// - Parameter bgColor: 背景颜色
    /// - Returns:
    func wj_bgColor(_ bgColor:UIColor) -> QMViewModel {
        guard self.wj_view != nil else {
            return self;
        }
        self.wj_view?.backgroundColor = bgColor;
        return self;
    }
}
// MARK:  label 属性
extension QMViewModel {
    /// 设置label的文字
    ///
    /// - Parameter text: 文字
    /// - Returns: 返回当前对象
    func wj_text(text:String) -> QMViewModel {
        type = wj_judgeViewType(wj_view: self.wj_view);
        if type == .label {
            let label =  self.wj_view as! UILabel;
            label.text = text;
            label.textAlignment = .center
        }
        return self;
    }
    
    /// 设置文字的显示位置
    ///
    /// - Parameter alignment: 显示的位置
    /// - Returns: 显示的文字
    func wj_textAlignment(_ alignment : NSTextAlignment) -> QMViewModel {
        guard wj_judgeViewType(wj_view: wj_view) == .label else {
            return self;
        }
        let label  = self.wj_view as! UILabel;
        label.textAlignment = alignment;
        return self;
    }
    
    /// 设置label的字体的大小
    ///
    /// - Parameter font: 字体的大小
    /// - Returns: 当前对象
    func wj_font(font:CGFloat) -> QMViewModel {
        guard wj_judgeViewType(wj_view: self.wj_view) == .label else {
            return self;
        }
        let label = self.wj_view as! UILabel;
        label.font = UIFont.systemFont(ofSize: font);
        return self;
    }
    
}
// MARK: Btton 属性赋值
extension QMViewModel {
    
    /// 设置buttontitl
    ///
    /// - Parameters:
    ///   - title: 按钮标题
    ///   - status: 显示的状态
    /// - Returns:
    func wj_btnTitle(_ title:String , status:UIControlState = .normal) -> QMViewModel {
        guard wj_judgeViewType(wj_view: self.wj_view) == .button else {
            return self;
        }
        let buton = self.wj_view as! UIButton;
        buton.setTitle(title, for: status);
        return self;
        
    }
    
    /// 设置按钮文字的颜色
    ///
    /// - Parameters:
    ///   - titleColor: 文字的颜色
    ///   - status: 各个状态
    /// - Returns: 当前对象
    func wj_btnTitleColor(_ titleColor:UIColor,status : UIControlState = .normal) -> QMViewModel {
        guard wj_judgeViewType(wj_view: self.wj_view) == .button else {
            return self;
        }
        let buton = self.wj_view as! UIButton;
        buton.setTitleColor(titleColor, for: status);
        return self;
    }
    
    /// 设置btnImage
    ///
    /// - Parameters:
    ///   - imageName: 图片的名称
    ///   - status: 显示在那个状态下面
    /// - Returns: 返回对象
    func wj_btnImage(_ imageName:String , status:UIControlState = .normal) -> QMViewModel {
        guard wj_judgeViewType(wj_view: self.wj_view) == .button else {
            return self;
        }
        let buton = self.wj_view as! UIButton;
        buton.setImage(UIImage.init(named: imageName), for: status);
        return self;
    }
    
    /// 设置button的按钮的北京图片
    ///
    /// - Parameters:
    ///   - imageName: 按钮的背景图片
    ///   - status: 背景图片显示的名称
    /// - Returns: 返回当前的对象
    func wj_btnBgImage(_ imageName:String,status:UIControlState = .normal) -> QMViewModel {
        
        guard wj_judgeViewType(wj_view: self.wj_view) == .button else {
            return self;
        }
        let buton = self.wj_view as! UIButton;
        buton.setBackgroundImage(UIImage.init(named: imageName), for: status);
        return self;
    }
    
    /// 按钮添加点击方法
    ///
    /// - Parameters:
    ///   - action: 点击的回掉闭包
    ///   - events: 必要响应事件
    /// - Returns: 返回当前对象
    func wj_btnAction(action :@escaping QMBtnAction ,events :UIControlEvents  = .touchUpInside) -> QMViewModel {
        guard wj_judgeViewType(wj_view: self.wj_view) == .button else {
            return self;
        }
        let buton = self.wj_view as! UIButton;
        buton.qm_btnAction = action;
        buton.addTarget(buton, action: #selector(UIButton.wj_buttonAction(button:)), for: .touchUpInside)
        return self;
    }
    
    /// 设置button的imageEdgeInsets
    ///
    /// - Parameter qm_Edge: imageEdgeInsets
    /// - Returns:
    func wj_btnImageEdge(edge qm_Edge:UIEdgeInsets) -> QMViewModel {
        assert(wj_judgeViewType(wj_view: self.wj_view) != .button, "不是button，那里来的imageEdgeInsets");
        let button = self.wj_view as! UIButton;
        button.imageEdgeInsets = qm_Edge;
        return self;
    }
    
    /// 设置button的titleEdgeInsets
    ///
    /// - Parameter qm_Edge: titleEdgeInsets
    /// - Returns: 
    func wj_btnTitleEdge(edge qm_Edge:UIEdgeInsets) -> QMViewModel {
        assert(wj_judgeViewType(wj_view: self.wj_view) != .button, "不是button，那里来的titleEdgeInsets");
        let button = self.wj_view as! UIButton;
        button.titleEdgeInsets = qm_Edge;
        return self;
    }
}


